package com.zxn.dorm.view.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.zxn.dorm.ICommonEventKey;
import com.zxn.dorm.MjyApp;
import com.zxn.dorm.R;
import com.zxn.dorm.entity.MemberBeanInfo;
import com.zxn.dorm.view.dialog.ClockNameEditDgFrag;
import com.zxn.iconitemview.IconItemView;
import com.zxn.presenter.model.CommonEvent;
import com.zxn.presenter.model.CommonEventBus;
import com.zxn.presenter.view.BaseActivity;
import com.zxn.titleview.TitleView;
import com.zxn.utils.UIUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 会员信息编辑页面.
 * Created by zxn on 2019-6-11 11:23:14.
 */
public class MemberEditActivity extends BaseActivity implements ClockNameEditDgFrag.OnDialogClickListener {

    private static final String ARG_PARAM1 = "param1";
    @BindView(R.id.title_common)
    TitleView titleCommon;
    //    @BindView(R.id.iiv_level_name)
//    IconItemView iivLevelName;
    @BindView(R.id.iiv_member_name)
    IconItemView iivMemberName;
    @BindView(R.id.iiv_birthday)
    IconItemView iivBirthday;
    @BindView(R.id.iv_head_img_url)
    ImageView ivHeadImgUrl;
    @BindView(R.id.iiv_mobile)
    IconItemView iivMobile;
    private Long mId;
    private MemberBeanInfo memberBeanInfo;
    private int mItemId;

    public static void jumpTo(Context context, Long id) {
        Intent intent = new Intent(context, MemberEditActivity.class);
        intent.putExtra(ARG_PARAM1, id);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_member_edit;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mId = getIntent().getLongExtra(ARG_PARAM1, 0);

        initTitle();
        memberBeanInfo = MjyApp.mMjyApp.mMemberBeanInfoDao.load(mId);
        if (null != memberBeanInfo) {
            iivMemberName.setRightText(memberBeanInfo.memberName);
            iivMobile.setRightText(memberBeanInfo.mobile);
            iivBirthday.setRightText(memberBeanInfo.sex == 0 ? "男" : "女");
        }

    }

    private void initTitle() {
        TextView view = new TextView(this);
        view.setTextColor(UIUtils.getColor(R.color.c_ffffff));
        view.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
        view.setText("保存");
        view.setOnClickListener(v -> {
            MjyApp.mMjyApp.mMemberBeanInfoDao.update(memberBeanInfo);
            CommonEventBus.post(new CommonEvent<>(ICommonEventKey.EVENT_UPDATE));
            showToast("保存成功");
            finish();
        });
        titleCommon.addRightView(view);
    }


    @OnClick({R.id.iiv_member_name, R.id.iiv_birthday, R.id.iiv_mobile})
    public void onViewClicked(View view) {
        mItemId = view.getId();
        switch (view.getId()) {
            case R.id.iiv_member_name:
                ClockNameEditDgFrag.newInstance(this, iivMemberName.getTitleView().getText().toString()).show(getSupportFragmentManager());
                break;
            case R.id.iiv_birthday:
                //ClockNameEditDgFrag.newInstance(this, iivBirthday.getTitleView().getText().toString()).show(getSupportFragmentManager());
                break;
            case R.id.iiv_mobile:
                ClockNameEditDgFrag.newInstance(this, iivMobile.getTitleView().getText().toString()).show(getSupportFragmentManager());
                break;
        }
    }


    @Override
    public void onConfirmClick(String num) {
        switch (mItemId) {
            case R.id.iiv_member_name:
                iivMemberName.setRightText(num);
                memberBeanInfo.memberName = num;
                break;
            case R.id.iiv_birthday:
//                iivBirthday.setRightText(num);
//                memberBeanInfo.sex = num;
                break;
            case R.id.iiv_mobile:
                iivMobile.setRightText(num);
                memberBeanInfo.mobile = num;
                break;
        }
    }
}
